#include "keypresshandler.h"
#include <QKeyEvent>
#include <QDebug>
#include <QEvent>
#include <QToolTip>
#include <QTextEdit>

KeyPressHandler::KeyPressHandler(QObject *parent) : QObject(parent)
{

}

bool KeyPressHandler::eventFilter(QObject *obj, QEvent *event)
  {
      if (event->type() == QEvent::KeyPress) {
          QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
          QTextEdit* edit = static_cast <QTextEdit*>(obj);

          QString text = keyEvent->text();
          uint len = text.size();
          if(len)
          {
              QChar ch;
              QByteArray dat = text.toLocal8Bit();
              QString tipText("只允许输入16进制数");
              QRect rect=edit->cursorRect();

              ch =* dat.data();
              if( ((ch>32) && (ch<48)) ||\
                  ((ch>57) && (ch<65)) ||\
                  ((ch>70) && (ch<97)) ||\
                  ((ch>102) && (ch<=126)) )
              {

                  QToolTip::showText(edit->mapToGlobal(QPoint(0,0))+ rect.topLeft(),tipText,NULL,QRect(),1000);
                  return true;
              }
//              else if((ch>57) && (ch<65))
//              {

//                  QToolTip::showText(edit->mapToGlobal(QPoint(0,0))+ rect.topLeft(),tipText);
//                  return true;
//              }
//              else if((ch>70) && (ch<97))
//              {

//                  QToolTip::showText(edit->mapToGlobal(QPoint(0,0))+ rect.topLeft(),tipText);
//                  return true;
//              }
//              else if((ch>102) && (ch<=126))
//              {

//                  QToolTip::showText(edit->mapToGlobal(QPoint(0,0))+ rect.topLeft(),tipText);
//                  return true;
//              }
          }

      }
//      else if (event->type() == QEvent::InputMethod) {
//           QInputMethodEvent *methodEvent = static_cast<QInputMethodEvent *>(event);

//           QString  str = methodEvent->commitString();

//      }
          // standard event processing
      return QObject::eventFilter(obj, event);

  }
